package com.beiming.libra.service.impl;

import com.beiming.libra.common.page.Page;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.UserMessageMapper;
import com.beiming.libra.domain.UserMessage;
import com.beiming.libra.service.UserMessageService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: tyrion
 * @Date: 2018/6/5 11:58
 * @类描述:
 */
@Service
public class UserMessageServiceImpl implements UserMessageService {

  @Autowired
  UserMessageMapper  userMessageMapper;

  public UserMessage getMessageById(String id) {
    return userMessageMapper.getMessageById(id);
  }


  public PageResult<UserMessage> listWithCondition(UserMessage um) {
    Page page = new Page();
    page.setPageNo(um.getPageNo());
    um.setPageNo((um.getPageNo() - 1) * um.getPageSize());
    PageResult<UserMessage> pageResult = new PageResult<UserMessage>();
    List<UserMessage> list = userMessageMapper.listWithCondition(um);

    int total = userMessageMapper.countList(um);
    page.setPageSize(um.getPageSize());
    page.setTotalRecords(total);
    pageResult.setPage(page);
    pageResult.setList(list);
    return pageResult;
  }

  public void updateStatusById(String id) {
    userMessageMapper.updateStatusById(id);
  }

  public void insertUserMessage(UserMessage um) {
    if (um.getId() == null) {
      String id = CommonsUtils.get32BitUUID();
      um.setId(id);
    }
    userMessageMapper.insertUserMessage(um);
  }
}
